package goquery

import (
	
)

const (
	maxUint = ^uint(0)
	maxInt  = int(maxUint >> 1)

	// ToEnd is a special index value that can be used as end index in a call
	// to Slice so that all elements are selected until the end of the Selection.
	// It is equivalent to passing (*Selection).Length().
	ToEnd = maxInt
)

// First reduces the set of matched elements to the first in the set.
// It returns a new Selection object, and an empty Selection object if the
// the selection is empty.
func ( *Selection) () *Selection {
	return .Eq(0)
}

// Last reduces the set of matched elements to the last in the set.
// It returns a new Selection object, and an empty Selection object if
// the selection is empty.
func ( *Selection) () *Selection {
	return .Eq(-1)
}

// Eq reduces the set of matched elements to the one at the specified index.
// If a negative index is given, it counts backwards starting at the end of the
// set. It returns a new Selection object, and an empty Selection object if the
// index is invalid.
func ( *Selection) ( int) *Selection {
	if  < 0 {
		 += len(.Nodes)
	}

	if  >= len(.Nodes) ||  < 0 {
		return newEmptySelection(.document)
	}

	return .Slice(, +1)
}

// Slice reduces the set of matched elements to a subset specified by a range
// of indices. The start index is 0-based and indicates the index of the first
// element to select. The end index is 0-based and indicates the index at which
// the elements stop being selected (the end index is not selected).
//
// The indices may be negative, in which case they represent an offset from the
// end of the selection.
//
// The special value ToEnd may be specified as end index, in which case all elements
// until the end are selected. This works both for a positive and negative start
// index.
func ( *Selection) (,  int) *Selection {
	if  < 0 {
		 += len(.Nodes)
	}
	if  == ToEnd {
		 = len(.Nodes)
	} else if  < 0 {
		 += len(.Nodes)
	}
	return pushStack(, .Nodes[:])
}

// Get retrieves the underlying node at the specified index.
// Get without parameter is not implemented, since the node array is available
// on the Selection object.
func ( *Selection) ( int) *html.Node {
	if  < 0 {
		 += len(.Nodes) // Negative index gets from the end
	}
	return .Nodes[]
}

// Index returns the position of the first element within the Selection object
// relative to its sibling elements.
func ( *Selection) () int {
	if len(.Nodes) > 0 {
		return newSingleSelection(.Nodes[0], .document).PrevAll().Length()
	}
	return -1
}

// IndexSelector returns the position of the first element within the
// Selection object relative to the elements matched by the selector, or -1 if
// not found.
func ( *Selection) ( string) int {
	if len(.Nodes) > 0 {
		 := .document.Find()
		return indexInSlice(.Nodes, .Nodes[0])
	}
	return -1
}

// IndexMatcher returns the position of the first element within the
// Selection object relative to the elements matched by the matcher, or -1 if
// not found.
func ( *Selection) ( Matcher) int {
	if len(.Nodes) > 0 {
		 := .document.FindMatcher()
		return indexInSlice(.Nodes, .Nodes[0])
	}
	return -1
}

// IndexOfNode returns the position of the specified node within the Selection
// object, or -1 if not found.
func ( *Selection) ( *html.Node) int {
	return indexInSlice(.Nodes, )
}

// IndexOfSelection returns the position of the first node in the specified
// Selection object within this Selection object, or -1 if not found.
func ( *Selection) ( *Selection) int {
	if  != nil && len(.Nodes) > 0 {
		return indexInSlice(.Nodes, .Nodes[0])
	}
	return -1
}